/**
 * 定义模块
 */
$(function() {
	app = new Vue({
		el : '#app',
		/**
		 * 定义数据
		 */
		data : {
			user : {}
		},
		/**
		 * 定义方法
		 */
		methods : {
			/**
			 * 查询
			 */
			getIdFromUrl:function(){
				var addr = location.href;
				var index = addr.lastIndexOf('/') + 1;
				var id = addr.substr(index);
				return id;
			},
			 closeWin: function() {
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
			},

			finduser:function () {
				var id = this.getIdFromUrl();
				$.ajax({
				    type: "GET",
				    url: '/manager/usermanager/get/'+id,
				    beforeSend: function () {
				    },
				    complete: function () {
				    },
				    error: function(request) {
				        alert("Connection error");
				    },
				    success: function(result) {
				        if (result.status == 0) {
				        	var data = result.data;
				        	app.user = data;
//				        	for(var i = 0; i < data.roles.length; i++) {
//				        		$(":checkbox[name=roles][value="+data.roles[i].id+"]").prop("checked", true);
//				        	}
				        	
				        }
				    }
				});
			},
			query : function(pageNo, pageSize) {
				var _self = this;
				if (!pageNo) {
					pageNo = 1;
				}
				if (!pageSize) {
					pageSize = $("#pageSizeSelect").val();
				}
				var tip = layer.load(0, {
					shade : false
				});
				$.ajax({
					type : "POST",
					url : '/manager/usermanager/query',
					data : {
						pageNo : pageNo,
						pageSize : pageSize
					},
					beforeSend : function() {
					},
					complete : function() {
						layer.close(tip);
					},
					error : function(request) {
						layer.alert("系统错误");
					},
					success : function(result) {
						if (result.status == 0) {
							var data = result.data;
							_self.pageData = data;
//							laypage({
//								cont : 'pageNav',
//								pages : data.totalPage,
//								curr : data.currentPage,
//								skip : true,
//								groups : 6,
//								jump : function(obj, first) {
//									if (!first) {
//										_self.query(obj.curr);
//									}
//								}
//							});
						} else {
							sessionCheckTimeOut(result);
						}
					}
				});
			},
			/**
			 * 新增
			 */
			add : function() {
				layer_show('新增会员', '/member/user/add', '', '500');
			},
			/**
			 * 修改
			 */
			edit : function(id) {
				layer_show('编辑会员', '/manager/usermanager/edit/' + id, '', '500');
			},
			/**
			 * 查看
			 */
			view : function(id) {
				layer_show('查看', '/manager/usermanager/view/' + id, '', '800');
			},			
			/*
			 * 删除
			 */
			del : function(id) {
				_self = this;
				layer.confirm('确认要删除吗？', function() {
					var tipIndex = null;
					$.ajax({
						type : "POST",
						url : '/member/user/delete/' + id,
						beforeSend : function() {
							tipIndex = layer.load(0, {
								shade : false
							});
						},
						complete : function() {
							layer.close(tipIndex);
						},
						error : function(request) {
							layer.alert("系统出错");
						},
						success : function(data) {
							layer.alert(data.message);
							if (data.status == 0) {
								_self.query();
							}
						}
					});
				});
			},
			/**
			 * 批量删除
			 */
			batchDel : function() {
				_self = this;
				var selectdItems = $(":checkbox[name=ids]:checked");
				if (selectdItems.size() == 0) {
					layer.alert("请选择要删除的记录！");
					return;
				}
				var ids = [];
				selectdItems.each(function() {
					ids.push($(this).val());
				});

				layer.confirm('确认要删除吗？', function() {
					var tipIndex = null;
					$.ajax({
						type : "POST",
						url : '/member/user/deletes',
						data : {
							ids : ids
						},
						beforeSend : function() {
							tipIndex = layer.load(0, {
								shade : false
							});
						},
						complete : function() {
							layer.close(tipIndex);
						},
						error : function(request) {
							layer.alert("系统出错");
						},
						success : function(data) {
							layer.alert(data.message);
							if (data.status == 0) {
								_self.query();
							}
						}
					});
				});

			},
			// 初始化
			init : function() {
				this.finduser();
			}
		}
	});

	app.init();
});